package com.liu.manager.service.impl;

import com.liu.manager.bean.ResponseResult;
import com.liu.manager.entity.UserAndRoler;
import com.liu.manager.mapper.UserAndRolerMapper;
import com.liu.manager.service.UserAndRolerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserAndRolerSvcImpl implements UserAndRolerService {

    @Resource
    UserAndRolerMapper userAndRolerMapper;

    @Override
    public ResponseResult insertUserAndRoler(UserAndRoler userAndRoler) {
        if(userAndRoler.getRolerId() <= 0){
            return ResponseResult.ERROR("角色id错误");
        }
        if(userAndRoler.getUserId() <= 0){
            return ResponseResult.ERROR("用户id错误");
        }
        userAndRoler.setCreateTime(System.currentTimeMillis());
        long id = userAndRolerMapper.insertUserAndRoler(userAndRoler);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(userAndRoler);
        return responseResult;
    }

    @Override
    public ResponseResult findByUserId(long userId) {
        if(userId <= 0){
            return ResponseResult.ERROR("用户id错误");
        }
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("查询成功");
        responseResult.setResult(userAndRolerMapper.findByUserId(userId));
        return responseResult;
    }

    @Override
    public ResponseResult findById(long id) {
        if(id <= 0){
            return ResponseResult.ERROR("id错误");
        }
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("查询成功");
        responseResult.setResult(userAndRolerMapper.findById(id));
        return responseResult;
    }

    @Override
    public ResponseResult findByRolerId(long rolerId) {
        if(rolerId <= 0){
            return ResponseResult.ERROR("用户id错误");
        }
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("查询成功");
        responseResult.setResult(userAndRolerMapper.findByRolerId(rolerId));
        return responseResult;
    }

    @Override
    public ResponseResult removeById(long id) {
        if(id <= 0){
            return ResponseResult.ERROR("id错误");
        }
        userAndRolerMapper.deleteUserAndRoler(id);
        return ResponseResult.OK();
    }
}
